package com.chuang.bootplus.controller.word;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chuang.bootplus.base.exception.BusException;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.entity.document.Document;
import com.chuang.bootplus.entity.topic.TMapW;
import com.chuang.bootplus.entity.word.*;
import com.chuang.bootplus.mapper.WordMapper;
import com.chuang.bootplus.model.DocumentModel;
import com.chuang.bootplus.model.TimeModel;
import com.chuang.bootplus.po.TopicPO;
import com.chuang.bootplus.po.WordPO;
import com.chuang.bootplus.service.*;
import com.chuang.bootplus.vo.WordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chang
 * @since 2022-06-21
 */

@Api(tags = {"Word"})
@RestController
@RequestMapping("/word")
public class WordController {

    @Autowired
    DocumentService documentService;

    @Autowired
    WordService wordService;

    @Autowired
    WMapDService wMapDService;


    @Autowired
    WMapTService wMapTService;

    @Autowired
    TMapWService tMapWService;

    @Autowired
    WMapTimeService wMapTimeService;

    @Autowired
    WMapWService wMapWService;


//    @Transactional
//    @PostMapping("/addAll")
//    @ApiOperation("word json  进行 load ")
//    public ApiResponse<Void> addWord(@RequestBody WordPO[] po){
//        for(WordPO t:po){
//            Word word = new Word();
//            word.setWordKey(t.getKey());
//            wordMapper.insert(word);
//            Long insert = word.getWordId();
//            StringBuffer str = new StringBuffer();
//            for(String s:t.getTime_dis()){
//                str.append(s).append(" ");
//            }
//            WMapTime wMapTime = new WMapTime();wMapTime.setWordId(insert);wMapTime.setTimeDis(str.toString().trim());
//            wMapTimeService.save(wMapTime);
//            for (String topicId:t.getTopic()){
//                WMapT wMapT = new WMapT();
//                wMapT.setTopicId(topicId);
//                wMapT.setWordId(insert);
//                wMapTService.save(wMapT);
//            }
//            //文档
//            for (Long dId:t.getDoc_list()){
//                WMapD wMapD = new WMapD();
//                wMapD.setDocumentId(dId);
//                wMapD.setWordId(insert);
//                wMapDService.save(wMapD);
//            }
//            //近义
//            for (Long closeId: t.getClose_word()){
//                WMapW wMapW = new WMapW();
//                wMapW.setMapCategory(1);
//                wMapW.setMapId(closeId);
//                wMapW.setWordId(insert);
//                wMapWService.save(wMapW);
//            }
//            //伴随
//            for (Long bscId: t.getBsc()){
//                WMapW wMapW = new WMapW();
//                wMapW.setMapCategory(2);
//                wMapW.setMapId(bscId);
//                wMapW.setWordId(insert);
//                wMapWService.save(wMapW);
//            }
//        }
//        return new ApiResponse<>();
//    }

//
//    @Transactional
//    @PostMapping("/addAll2")
//    @ApiOperation("load  Word 文件 ")
//    public ApiResponse<Void> addFile(@RequestBody TopicPO[] po) throws IOException {
//        String jsonStr = "";
//        try {
//            File jsonFile = new File("C:\\Users\\jokehe\\Desktop\\word.json");
//            FileReader fileReader = new FileReader(jsonFile);
//            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
//            int ch = 0;
//            StringBuffer sb = new StringBuffer();
//            while ((ch = reader.read()) != -1) {
//                sb.append((char) ch);
//            }
//            fileReader.close();
//            reader.close();
//            jsonStr = sb.toString();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//        System.out.println(jsonStr.toString());
//        List<WordPO> wordPOS = JSONObject.parseArray(jsonStr, WordPO.class);
//        System.out.println(wordPOS.size());
//
//        wordPOS.forEach(t->{
//            Word word = new Word();
//            word.setWordKey(t.getKey());
//            wordMapper.insert(word);
//            Long insert = word.getWordId();
//
//            int i=0;
//            for(String s:t.getTime_dis()){
//                String time="2021-0";
//                i++;
//                time+=String.valueOf(i);
//                wMapTimeService.save(new WMapTime().setWordId(insert).setTimeDis(s).setTime(time));
//            }
//
//        });
//        System.out.println("导入word完成");
//        return new ApiResponse<>();
//    }



    @Transactional
    @PostMapping("/getWordMessage")
    @ApiOperation("根据wordId获取相关信息 ")
    public ApiResponse<WordVO> addFile(@RequestParam Long wordId) throws IOException {
        Word one = wordService.getById(wordId);
        List<WMapTime> list = wMapTimeService.list(new LambdaQueryWrapper<WMapTime>().eq(WMapTime::getWordId, one.getWordId()));
        List<TimeModel> timeModels = BeanUtil.listA2ListB(list, TimeModel.class);//关联的time 和time_dis

        List<WMapT> list3 = wMapTService.list(new LambdaQueryWrapper<WMapT>().eq(WMapT::getWordId, one.getWordId()));
        List<String> topic = list3.stream().map(WMapT::getTopicId).collect(Collectors.toList());//关联的topic

        List<WMapD> list4 = wMapDService.list(new LambdaQueryWrapper<WMapD>().eq(WMapD::getWordId, one.getWordId()));
        List<Long> collect1 = list4.stream().map(WMapD::getDocumentId).collect(Collectors.toList());
        List<Document> doc_list = documentService.listByIds(collect1);//关联的Document
        List<DocumentModel> documentModels = BeanUtil.listA2ListB(doc_list, DocumentModel.class);


        List<WMapW> list1 = wMapWService.list(new LambdaQueryWrapper<WMapW>().eq(WMapW::getMapCategory, 1).eq(WMapW::getWordId, one.getWordId()));
        List<Long> collect = list1.stream().map(WMapW::getMapId).collect(Collectors.toList());
        List<Word> close_words=new ArrayList<>();
        if (collect.size()>0){
            close_words= wordService.listByIds(collect);//关联的close_words
            close_words.stream().forEach(t->{
                List<WMapTime> list2 = wMapTimeService.list(new LambdaQueryWrapper<WMapTime>().eq(WMapTime::getWordId, t.getWordId()));
                List<TimeModel> timeModels2 = BeanUtil.listA2ListB(list2, TimeModel.class);//关联的time 和time_dis
                t.setTimes(timeModels2);
            });
        }

        List<WMapW> list2 = wMapWService.list(new LambdaQueryWrapper<WMapW>().eq(WMapW::getMapCategory, 2).eq(WMapW::getWordId, one.getWordId()));
        List<Long> collect2 = list2.stream().map(WMapW::getMapId).collect(Collectors.toList());
        List<Word> bscs =new ArrayList<>();
        if (collect2.size()>0){
            bscs = wordService.listByIds(collect2);//关联的bscs
            bscs.stream().forEach(t->{
                List<WMapTime> list6 = wMapTimeService.list(new LambdaQueryWrapper<WMapTime>().eq(WMapTime::getWordId, t.getWordId()));
                List<TimeModel> timeModels6 = BeanUtil.listA2ListB(list6, TimeModel.class);//关联的time 和time_dis
                t.setTimes(timeModels6);
            });
        }

        WordVO wordVO = new WordVO();
        wordVO.setKey(one.getWordKey()).setTime_model(timeModels).setTopic(topic).setDoc_list(documentModels).setClose_word(close_words).setBsc(bscs);

        return new ApiResponse(wordVO);
    }


    @Transactional
    @PostMapping("/changeState")
    @ApiOperation("改变词状态 ")
    public ApiResponse<Void> change(@RequestParam String topId, @RequestParam Long wordId){
        TMapW one = tMapWService.getOne(new LambdaQueryWrapper<TMapW>().eq(TMapW::getTopicId, topId).eq(TMapW::getWordId, wordId));
        if (one.getCategoryId()==1) throw new BusException("无法改变当前词状态");
        one.setCategoryId(1);
        tMapWService.updateById(one);
        return new ApiResponse<>();
    }






}

